<?php
($user_id = session('user_id')) || abort('请先登录');
($goods_id = _POST('goods_id', 0)) || abort('参数错误');
($goods = get_goods_info($goods_id)) || abort('商品不存在');

/* 检查是否已经存在于用户的收藏夹 */
$sql = "SELECT COUNT(*) FROM " .$GLOBALS['ecs']->table('collect_goods') .
    " WHERE user_id={$user_id} AND goods_id={$goods_id}";
$GLOBALS['db']->GetOne($sql) > 0 && abort('已收藏过');

$time = gmtime();
$sql = "INSERT INTO " .$GLOBALS['ecs']->table('collect_goods'). " (user_id, goods_id, add_time)" .
            "VALUES ({$user_id}, {$goods_id}, {$time})";

$GLOBALS['db']->query($sql) === false && abort('收藏失败');
return [];
